package com.pos.cashier.emp.controller;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pos.cashier.basic.controller.base.BaseController;
import com.pos.cashier.basic.entity.Page;
import com.pos.cashier.basic.util.DateUtil;
import com.pos.cashier.basic.util.PageData;
import com.pos.cashier.emp.bean.Employee;
import com.pos.cashier.emp.service.EmployeeService;
@Controller
@RequestMapping(value="/emp")
public class EmployeeController extends BaseController {

	/*
	 * 类名称：MenuController
	 * 创建人：CKf
	 * 创建时间：2018年5月28日
	 * @version
	 */

		@Resource(name="employeeService")
		private EmployeeService employeeService;
		
		/**
		 * 显示用户列表
		 * @param model
		 * @return
		 */
		@RequestMapping(value="/empList")
		public ModelAndView listUsers(Page page)throws Exception{
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData(); 
			pd = this.getPageData(); 
			page.setPd(pd);
			List<PageData>	userList = employeeService.listPdPageUser(page);			//列出用户列表
			
			mv.setViewName("system/emp/emp_list");
			mv.addObject("userList", userList);
			mv.addObject("pd", pd); 
			return mv;
		}
		
		/**
		 * 去新增用户页面
		 */
		@RequestMapping(value="/toAdd")
		public ModelAndView goAddU()throws Exception{
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData();
			pd = this.getPageData();

			
			mv.addObject("pd", pd);
			mv.addObject("msg","add"); 
			mv.setViewName("system/emp/emp_edit");
			return mv;
		}
		
		/**
		 * 保存用户信息
		 * @param menu
		 * @param model
		 * @return
		 */
		@RequestMapping(value="/add") 
		@ResponseBody
		public JSONObject add(String  data,HttpServletResponse response)throws Exception{
			response.setContentType("text/html;charset=GBK");//解决中文乱码
			response.setCharacterEncoding("utf-8");  
			JSONObject jb = new JSONObject();
			try{ 
	        	JSONObject json_look = JSONObject.fromObject(data); 
	        	Employee employee = (Employee)JSONObject.toBean(json_look, Employee.class);
				String passwd = new SimpleHash("SHA-1", employee.getName(), employee.getPassword()).toString(); 
				employee.setPassword(passwd); 
				employee.setCreate_time(DateUtil.getTime());  
				employeeService.save(employee); 
				jb.accumulate("result", "success"); 
				//TODO 用户名重复
				//jb.accumulate("result", "failed");
			} catch(Exception e){
				logger.error(e.toString(), e); 
				jb.accumulate("result", "error");
			}
			return jb;
		}
		
		/**
		 * 请求编辑用户页面
		 * @param 
		 * @return
		 */
		@RequestMapping(value="/toEdit")
		public ModelAndView toEdit(String id)throws Exception{
			ModelAndView mv = this.getModelAndView();
			PageData pd = this.getPageData();
			try{
				pd.put("id",id);
				pd = employeeService.findByUiId(pd);
/*				List<Menu> menuList = employeeService.listAllParentMenu();
				mv.addObject("menuList", menuList);*/
				mv.addObject("pd", pd);
				mv.setViewName("system/emp/emp_edit");
			} catch(Exception e){
				logger.error(e.toString(), e);
			}
			return mv;
		}
		
		
		
		/**
		 * 保存编辑
		 * @param 
		 * @return
		 */
		@RequestMapping(value="/edit") 
		@ResponseBody
		public JSONObject edit(HttpServletResponse response,String data)throws Exception{ 
			response.setContentType("text/html;charset=GBK");//解决中文乱码
			response.setCharacterEncoding("utf-8");   
			JSONObject jo = new JSONObject();
			try{ 
	        	JSONObject json_look = JSONObject.fromObject(data); 
	        	Employee employee = (Employee)JSONObject.toBean(json_look, Employee.class); 
				String passwd = new SimpleHash("SHA-1", employee.getName(), employee.getPassword()).toString(); 
				employee.setPassword(passwd); 
				employeeService.update(employee);
				jo.accumulate("result", "success"); 
			} catch(Exception e){
				logger.error(e.toString(), e);
				jo.accumulate("result", "failed");
			}
			return jo;
		}
		
	
		
		
		/**
		 * 删除用户
		 * @param menuId
		 * @param out
		 */
		@RequestMapping(value="/del") 
		public String delete(@RequestParam String id,HttpServletResponse response)throws Exception{ 
			try{  
				Employee e = new Employee(); 
				e.setId(id); 
				e.setIs_del("1");
				employeeService.update(e);
			} catch(Exception e){
				logger.error(e.toString(), e); 
			} 
			return "redirect:/emp/empList";
		} 
		 
		public static void main(String[] args) {
			//计算和
			BigInteger num = new BigInteger("0");
			num = num.setBit(1);
			//2^1
			System.out.println(num);
			//2^2+2^1
			num = num.setBit(2);
			System.out.println(num);
			//2^3+2^2+2^1
			num = num.setBit(3);
			System.out.println(num);

			System.out.println(num.testBit(3));
			int sum = (int) (Math.pow(2, 1)+Math.pow(2, 2)+Math.pow(2, 3));
			System.out.println(sum);
			// 00000010
			//   2
			// 00001110
			System.out.println(sum & (int)(Math.pow(2, 3)));
		}

}
